package com.cd.music.utils;

import com.cd.music.dto.ResultBean;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yxl 余秀良
 * @version 1.0
 * @description: cookie操作工具类
 * @date 2018年03月13日  上午9:49
 * @address: 四川成都
 */
public final class CookieUtil {
    /**
     * @param name  cookie的名字
     * @param value 　cookie的值
     * @author yxl 余秀良
     * @version 1.0
     * @description: 添加cookie
     * @date 2018年03月13日 上午10:14
     * @address: 四川成都
     */
    public static ResultBean set(String name, String value) {
        ResultBean.Builder builder = ResultBean.builder();
        try {
            Cookie cookie = new Cookie(name, value);
            cookie.setPath("/");
            // 设置保存cookie时长
            cookie.setMaxAge(-1);
            saveCookie(cookie);
            builder.success(true).code(200).message("成功");
        } catch (Exception e) {
            builder.success(false).code(500).message("失败");
        }
        return builder.build();
    }

    /**
     * @param name cookie的名字
     * @author yxl 余秀良
     * @version 1.0
     * @description: 获取cookie
     * @date 2018年03月13日 上午10:09
     * @address: 四川成都
     */
    public static ResultBean get(String name) {
        ResultBean.Builder builder = ResultBean.builder();
        try {
            Cookie[] cookies = getCookies();
            for (int i = 0; i < (cookies == null ? 0 : cookies.length); i++) {
                if ((name).equalsIgnoreCase(cookies[i].getName())) {
                    builder.success(true).code(200).message("成功").data(cookies[i].getValue());
                }
            }
        } catch (Exception e) {
            builder.success(false).code(500).message("失败");
        }
        return builder.build();
    }

    /**
     * @param name cookie的名字
     * @author yxl 余秀良
     * @version 1.0
     * @description: 删除cookie
     * @date 2018年03月13日 上午10:09
     * @address: 四川成都
     */
    public static ResultBean remove(String name) {
        ResultBean.Builder builder = ResultBean.builder();
        try {
            Cookie[] cookies = getCookies();
            for (int i = 0; i < (cookies == null ? 0 : cookies.length); i++) {
                if ((name).equalsIgnoreCase(cookies[i].getName())) {
                    Cookie cookie = new Cookie(name, "");
                    cookie.setPath("/");
                    // 设置保存cookie最大时长为0，即使其失效
                    cookie.setMaxAge(0);
                    saveCookie(cookie);
                    builder.message("删除升高").code(200).success(true);
                }
            }
        } catch (Exception e) {
            builder.message("删除失败").code(500).success(false);
        }
        return builder.build();
    }

    private CookieUtil() {
        throw new UnsupportedOperationException("这是一个工具类不能实例化");
    }

    private static void saveCookie(Cookie cookie) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = attributes.getResponse();
        response.addCookie(cookie);
    }

    private static Cookie[] getCookies() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        return request.getCookies();
    }
}
